using System.Drawing;
using System.Windows.Forms;
using System;

namespace Woz
{
    partial class Toolbox
    {
        private class Button : System.Windows.Forms.Button
        {
            static Button()
            {
                Woz.Toolbox.Button.sHeight = 25;
                Woz.Toolbox.Button.sMarginTop = 3;
                Woz.Toolbox.Button.sMarginBottom = 0;

                Woz.Toolbox.Button.sBackColor = Color.Transparent;
                Woz.Toolbox.Button.sBorderColor = Color.LightGray;

                Woz.Toolbox.Button.sMouseOverBackColor = Color.FromArgb(193, 210, 238);
                Woz.Toolbox.Button.sMouseOverBorderColor = Color.FromArgb(49, 106, 197);

                Woz.Toolbox.Button.sCheckedBackColor = Color.FromArgb(255, 212, 139);
                Woz.Toolbox.Button.sCheckedBorderColor = Color.Orange;
            }

            public Button(System.Windows.Forms.FlowLayoutPanel pParentControl, Woz.Tool pTool)
            {
                // assign the button name and text to be the same as the tools name
                this.Name = pTool.Name;
                this.Text = pTool.Name;

                // set our image to the tools icon
                this.Image = Woz.ResourceManager.Instance.Images[pTool.IconFileName];

                // save a reference of this tool in the button
                this.Tag = pTool;

                // initialize the visual stuff
                this.Margin = new Padding(3, Woz.Toolbox.Button.sMarginTop, 3, Woz.Toolbox.Button.sMarginBottom);
                this.Size = new System.Drawing.Size(pParentControl.Width - 6, Woz.Toolbox.Button.sHeight);

                this.FlatStyle = FlatStyle.Flat;
                this.BackColor = Woz.Toolbox.Button.sBackColor;
                this.FlatAppearance.BorderColor = Woz.Toolbox.Button.sBorderColor;
                this.FlatAppearance.MouseOverBackColor = Woz.Toolbox.Button.sMouseOverBackColor;

                this.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
                this.TabStop = false;
                this.TextAlign = System.Drawing.ContentAlignment.BottomLeft;
                this.TextImageRelation = System.Windows.Forms.TextImageRelation.ImageBeforeText;
                this.UseVisualStyleBackColor = false;

                // add callbacks to some mouse events
                this.MouseEnter += new System.EventHandler(this.cMouseEnter);
                this.MouseLeave += new System.EventHandler(this.cMouseLeave);
                this.MouseDown += new MouseEventHandler(this.cMouseDown);
            }

            public static int TotalHeight
            {
                get
                {
                    return Woz.Toolbox.Button.sHeight + Woz.Toolbox.Button.sMarginTop + Woz.Toolbox.Button.sMarginBottom;
                }
            }

            public bool Checked
            {
                get
                {
                    return this.mChecked;
                }

                set
                {
                    // make sure there is a change
                    if (this.mChecked == value)
                    {
                        // there is no change so just leave
                        return;
                    }

                    // this is a new state so update our member
                    this.mChecked = value;

                    // and also our new colors
                    if (this.mChecked)
                    {
                        this.BackColor = Woz.Toolbox.Button.sCheckedBackColor;
                        this.FlatAppearance.BorderColor = Woz.Toolbox.Button.sCheckedBorderColor;
                    }

                    else
                    {
                        this.BackColor = Woz.Toolbox.Button.sBackColor;
                        this.FlatAppearance.BorderColor = Woz.Toolbox.Button.sBorderColor;
                    }
                }
            }

            private void cMouseEnter(object pSender, System.EventArgs pEventArgs)
            {
                // just set the border color on the toolbox button to the mouse over border color
                this.FlatAppearance.BorderColor = Woz.Toolbox.Button.sMouseOverBorderColor;
            }

            private void cMouseLeave(object pSender, System.EventArgs pEventArgs)
            {
                // check if this button is selected
                if (this.mChecked)
                {
                    // set the border color on the button
                    this.FlatAppearance.BorderColor = Woz.Toolbox.Button.sCheckedBorderColor;
                }

                // or check if its not
                else
                {
                    // set the border color on the button
                    this.FlatAppearance.BorderColor = Woz.Toolbox.Button.sBorderColor;
                }
            }

            private void cMouseDown(object sender, MouseEventArgs e)
            {
                // throw the focus to the parent
                this.Parent.Focus();
            }

            private bool mChecked;

            private static int sHeight;
            private static int sMarginTop;
            private static int sMarginBottom;

            private static Color sBackColor;
            private static Color sBorderColor;
            
            private static Color sMouseOverBackColor;
            private static Color sMouseOverBorderColor;
            
            private static Color sCheckedBackColor;
            private static Color sCheckedBorderColor;
        }
    }
}